#!/bin/bash
# tester for other programs in pmu-tools
# PERF=... override perf binary
# NORES=1 don't check measurement results

PYTHON=${PYTHON:-python3}

. ./cpumap.sh
set -e
PERF=${PERF:-perf}
failed() {
	echo FAILED
}
PATH=$PATH:.
trap failed ERR 0
set -x

# XXX cgroup untested
for args in "" "-A" "--per-socket" "--per-core" "-r2" ; do

# interval-normalize.py

${PERF} stat -e cycles,branches,instructions,branch-misses,context-switches,page-faults -I100 $args -a -x, -o x$$.csv sleep 1

$WRAP interval-normalize.py --error-exit x$$.csv
$WRAP interval-normalize.py --error-exit x$$.csv > y$$.csv

grep -vq PARSE-ERROR y$$.csv
if [ -z "$NORES" ] ; then
for i in branch-misses branches context-switches cycles instructions page-faults ; do
	grep -q $i y$$.csv
done
fi

grep -vq PARSE-ERROR x$$.csv

# plot-normalized.py
$WRAP plot-normalized.py -o x$$-2.png y$$.csv

# interval-plot.py
$WRAP interval-plot.py x$$.csv -o x$$.png

done

DYGRAPHS=""

# original url http://dygraphs.com/1.0.1/dygraph-combined.js disappeared
if [ ! -r dygraph-combined.js ] && wget https://cdnjs.cloudflare.com/ajax/libs/dygraph/1.0.1/dygraph-combined.js ; then
DYGRAPHS=1
fi

for args in "-l2" "--all -v" "-l3 --single-thread" "--all -a -A"; do

FORCEHT=1 $WRAP toplev.py -v --force-cpu ${DCPU:-hsw} --nodes +CPU_Utilization -I 100 $args -o x$$.csv -x, ./workloads/BC1s
$WRAP toplev.py -v --force-cpu ${DCPU:-hsw} --nodes +CPU_Utilization -I 100 $args -o xn$$.csv -x, ./workloads/BC1s
$WRAP interval-normalize.py --error-exit < x$$.csv
$WRAP interval-normalize.py --error-exit < x$$.csv > y$$.csv

grep -vq PARSE-ERROR y$$.csv
[ -z "$NORES" ] && grep Frontend y$$.csv

$WRAP interval-normalize.py --normalize-cpu --error-exit < x$$.csv > yc$$.csv
[ -z "$NORES" ] && grep Frontend yc$$.csv

$WRAP interval-normalize.py --normalize-cpu --error-exit < xn$$.csv > yc$$.csv
[ -z "$NORES" ] && grep Frontend yc$$.csv

if grep -q CPUs x$$.csv ; then

$WRAP utilized.py x$$.csv -o y$$.csv
[ -z "$NORES" ] && grep Frontend y$$.csv

fi

if grep -q CPUs xn$$.csv ; then

$WRAP utilized.py xn$$.csv -o y$$.csv
[ -z "$NORES" ] && grep Frontend y$$.csv

fi

$WRAP interval-plot.py x$$.csv -o x$$.png

# plot-normalized.py
$WRAP plot-normalized.py -o x$$-2.png y$$.csv

# tl-serve.py
if [ -n "$DYGRAPHS" ] ; then
	$WRAP tl-serve.py --gen tls$$ x$$.csv
	rm -rf tls$$
fi

# tl-barplot.py
$WRAP tl-barplot.py x$$.csv -o x$$.png

rm x$$.png x$$-2.png

done

$WRAP tl-serve.py x$$.csv &
sleep 1
unset http_proxy
curl http://localhost:9001 > /dev/null
kill %1
sleep 1
wait %1

$PYTHON csv_formats.py

rm x$$.csv xn$$.csv

# cputop.py

$WRAP cputop.py "socket == 0"
$WRAP cputop.py "thread == 0 and socket == 0"
$WRAP cputop.py "thread == 1" offline
$WRAP cputop.py offline online
[ $($WRAP cputop.py True | wc -l | cut -d ' ' -f 1) -eq $(getconf _NPROCESSORS_ONLN) ]

# list-events.py

EVENTMAP=${cpus[hsw]} $WRAP list-events.py > x$$.lst
[ $(wc -l x$$.lst | cut -d ' ' -f 1) -gt 20 ]
grep -qi rtm_retired.aborted x$$.lst
rm x$$.lst

# event-translate.py
EVENTMAP=${cpus[hsw]} $WRAP event-translate.py r4c9 | grep -q rtm_retired.aborted

$WRAP gen-dot.py simple > /dev/null
$WRAP gen-dot.py ivb_client_ratios > /dev/null

# untested: counterdiff.py

# may need network:
# untested: event_download.py

# need root:
# untested: msr.py
# untested: pci.py
# untested: event-rmap.py

trap "" ERR 0

echo SUCCEEDED
